export default {
  data() {
    return {
      type: '',//上个页面传过来
      certified: false, //是否认证, 默认未认证false ,true认证
      notCertified: false, //未认证-完善姓名，身份证号  false第一步，true第二步

      names: '', //认证-持卡人-姓名
      idCardNos: '', //身份证号
      // bankcard: '6217996630003580085',
      // bankcard: '6230720280010160772',
      // bankcard: '6217853100026915504',
      bankcard: '',
      cardType: {}, //卡类型

      realname: '', //未认证-真实姓名
      idnumber: '', //未认证-身份证号
      phonenumber: '', //未认证-手机号码
      verificationCode: '', //未认证-验证码
      showmobile: false, //手机号弹窗
      showbussins: false, //服务协议弹窗
      showvcode: false, //验证码弹窗
      phonemobiles: '', //未认证-手机号码加密
      vcodetrue: false, //验证码错误
      showpeople: false, //持卡人弹框
      closyzmbtn: false, //是否关闭验证码弹框弹框
      // 倒计时
      tips: '',
      // refCode: null,
      seconds: 60,
      brankimg: '', //上传的银行卡图片
      isDel: false
    }
  },
  onBackPress() {
    if (!this.notCertified) {
      return false
    } else {
      this.notCertified = false
      return true
    }
  },

  onLoad(op) {
    if(op.type){
      this.type = op.add
    }
    //获取是否认证
    const value = uni.getStorageSync('idcards')
    if (value !== '') {
      this.certified = value
      console.log(this.certified)
    }
    const values = uni.getStorageSync('Ztc_C_USER_INFO')
    if (values.trueName !== null) {
      this.names = values.trueName.replace(/.(?=.)/g, '*')
    }
  },
  created() {
    this.CheckIDCardVerify() //判断身份证上传接口
    this.GetUserIdentityInfo()
  },
  methods: {
    //提交
    comfig() {
      // uni.navigateTo({
      // 	url:'/pages_fundModule/paymentPassword/paymentPassword'
      // })
      this.AddNewBankCard()
    },
    del() {
      this.bankcard = ''
    },
    // 格式化卡号显示，每4位加-
    formatCardNumber() {

    },
    //倒计时
    codeChange(text) {
      console.log(text)
      this.tips = text
    },
    end() {
      // this.$u.toast('倒计时结束')
    },
    start() {
      this.$u.toast('已发送')
    },
    // 点击导航栏右侧
    onNavigationBarButtonTap() {
      console.log(123)
      uni.navigateTo({
        url: '/pages_fundModule/supportingBanks/supportingBanks'
      })
    },
    //判断身份证上传接口
    async CheckIDCardVerify() {
      try {
        const res = await this.$api.commodity.CheckIDCardVerify({})
        if (res.code === 200) {
          this.certified = res.data
          uni.setStorage({
            key: 'idcards',
            data: this.certified,
          })
        }
      } catch (err) {}
    },
    // 下一步
    nextstep() {
      if (this.bankcard.length < 14) {
        uni.showToast({
          title: '请输入合理的银行卡卡号',
          duration: 3000,
          icon: 'none'
        })
      } else {
        this.GetBankInfo()
      }
    },
    //通过卡号获取银行卡信息
    async GetBankInfo() {
      try {
        const res = await this.$api.fundModule.GetBankInfo({
          bankNum: this.bankcard
        })
        this.cardType = res.data
        this.realname = this.cardType.trueName
        console.log(res, '信息')
        if (res.code === 200) {
          this.notCertified = true //未认证-完善姓名，身份证号  false第一步，true第二步
        }

      } catch (err) {}
    },

    //添加银行卡
    async AddNewBankCard() {
      try{
        let data = {
          bankNum: this.bankcard
        }
        const res = await this.$api.fundModule.AddNew(data)
        console.log(res,'123123123')
        if (res.code === 200) {
          if(this.type==='add'){
            this.$refs.uToast.show({
						  title: '添加成功',
						  type: 'success',
						  back: true
            })
          }else{
            this.$refs.uToast.show({
						  title: '添加成功',
						  type: 'success',
						  url: '/pages_fundModule/withdrawal/withdrawal'
            })
          }

        }
        if (res.code !== 200) {
          uni.showToast({
            icon: 'none',
            title: res.msg
          })
        }
      }catch(e){
        //TODO handle the exception
      }
      /* uni.request({
				url: 'http://192.168.0.125:88/api/client/core/AccountBank/AddNewBankCard',
				method: 'POST',
				data: {
					bankNum: this.bankcard
				},
				header: {
					'content-type': 'application/json; charset=utf-8',
					'Auth': uni.getStorageSync('token')
				},
				success: res => {
					if (res.data.code === 200) {
						this.$refs.uToast.show({
							title: '添加成功',
							type: 'success',
							url: '/pages_fundModule/withdrawal/withdrawal'
						})
					}
					if (res.data.code !== 200) {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				},
				fail: err => {
					console.log(err, 'err')

				}
			}) */
      /* try {
        const res = await this.$api.fundModule.AddNewBankCard({
          bankNum: this.bankcard,
          bankMobile: this.phonenumber,
          identityCode: this.verificationCode,
          trueName: this.realname,
          idCard: this.idnumber
        })
				console.log(res)
        if (res.code === -4) {
          if (res.msg === '验证码错误') {
            this.vcodetrue = true
          } else if (res.msg === '验证码已过期,请重新发送') {
            this.vcodetrue = true
          } else {
            this.vcodetrue = false
            this.showvcode = false
          }
        }
        if (res.code === 200) {
          this.vcodetrue = false
          const value = uni.getStorageSync('setpaymentPassword')
          if (value) {
            uni.switchTab({
              url: '/pages/index/index'
            })
          } else {
            uni.setStorage({
              key: 'setpaymentPassword',
              data: false,
            })
            uni.navigateTo({
              url: '/pages_fundModule/paymentPassword/paymentPassword'
            })
          }
        }

      } catch (err) {} */
    },

    //持卡人弹框
    showpeoplebtn() {
      this.showpeople = true
    },
    //验证码
    async GetUserIdentityInfo() {
      try {
        const res = await this.$api.fundModule.GetUserIdentityInfo({})
        this.names = res.data.trueName
        this.idCardNos = res.data.idCardNo

      } catch (err) {}
    },

    //拍照银行卡照片
    bankcamera() {
      uni.chooseImage({
        count: 1,
        sourceType: ['album', 'camera'],
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        success: result => {
          uni.showLoading({
            title: '图片上传中~',
          })
          this.$api.files.uploadImg({
            filePath: result.tempFilePaths[0], // 图片流
          }).then(res => {
            var urls = res.baseUrl + res.url
            this.brankimg = urls
            this.GetBankCardNumByImg()
            uni.hideLoading()
            uni.showLoading({
              title: '读取卡号中~',
              mask: true
            })
          })
        },
        error: function(e) {}
      })
    },
    //识别银行卡号
    async GetBankCardNumByImg() {
      try {
        const res = await this.$api.fundModule.GetBankCardNumByImg({
          img: this.brankimg
        })
        if (res.data === '') {
          this.$refs.uToast.show({
            title: '请上传正确且清晰的银行卡照片',
            icon: false,
            type: 'warning'
          })
        } else {

          uni.hideLoading()
          this.bankcard = res.data.num
        }

      } catch (err) {}
    },
  }
}
